package com.realphim.app.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.realphim.app.ActivityDetailsPhim;
import com.realphim.app.R;
import com.realphim.app.model.ModelPhim;
import com.realphim.app.util.MyCache;

public class AdapterPhimCategory extends BaseAdapter {

    public static final String TAG = "AdapterPhimCategory";

    private LayoutInflater inflater = null;

    private List<ModelPhim> listPhim;

    private int maxLenght = 0;

    private Context _context;

    public AdapterPhimCategory(Context context, List<ModelPhim> listPhim) {
        this.listPhim = listPhim;
        inflater = ((Activity) context).getLayoutInflater();
        maxLenght = context.getResources().getInteger(R.integer.max_char);
        _context = context;
    }

    @Override
    public int getCount() {
        return listPhim.size();
    }

    @Override
    public ModelPhim getItem(int position) {
        return listPhim.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class Holder {
        ImageView ivThumbnail;

//        TextView tvTitle;
//
//        TextView tvTitleTranslate;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) {
            vi = inflater.inflate(R.layout.row_phim, null);
            Holder holder = new Holder();
            holder.ivThumbnail = (ImageView) vi.findViewById(R.id.ivThumbnail);
//            holder.tvTitle = (TextView) vi.findViewById(R.id.tvTitle);
//            holder.tvTitleTranslate = (TextView) vi
//                    .findViewById(R.id.tvTitleTranslate);
            vi.setTag(holder);
        }
        ModelPhim phim = listPhim.get(position);
        Holder holder = (Holder) vi.getTag();

        if (phim.thumbnailBitmap != null) {
            holder.ivThumbnail.setImageBitmap(phim.thumbnailBitmap);
        } else {
            holder.ivThumbnail.setImageBitmap(null);
        }
        // String title = phim.title;
        // String titleTranslate = phim.titleTranslate;
        // if (phim.title.length() > maxLenght) {
        // title = phim.title.substring(0, maxLenght);
        // }
        // holder.tvTitle.setText(title);
        //
        // if (titleTranslate.length() > maxLenght) {
        // titleTranslate = titleTranslate.substring(0, maxLenght);
        // }
        // holder.tvTitleTranslate.setText(titleTranslate);

        holder.ivThumbnail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyCache.getInstance().currentPhim = listPhim.get(position);
                Intent i = new Intent(_context, ActivityDetailsPhim.class);
                _context.startActivity(i);
                ((Activity) _context).overridePendingTransition(
                        R.anim.slide_in_left, R.anim.slide_out_left);
            }
        });

        return vi;

    }
}
